#include "nichgvc.h"

int sgn(long a) {
	if (a > 0) {
		return +1;
	} else if (a < 0) {
		return -1;
	} else {
		return 0;
	}
}

void Line2d(int a, int b, int c, int d, int col) {
	long u;
	long s;
	long v;
	long d1x;
	long d1y;
	long d2x;
	long d2y;
	long m;
	long n;
	int  i;
	
	u = c - a;
	v = d - b;
	d1x = sgn(u);
	d1y = sgn(v);
	d2x = sgn(u);
	d2y = 0;
	m = abs(u);
	n = abs(v);
	
	if (m <= n) {
		d2x = 0;
		d2y = sgn(v);
		m = abs(v);
		n = abs(u);
	}
	
	s = (int)(m/2);
	
	for (i = 0; i < (int)m; i++) {
		vcscreen[(b*320) + a] = col;
		s += n;
		if (s >= m) {
			s -= m;
			a += d1x;
			b += d1y;
		} else {
			a += d2x;
			b += d2y;
		}
	}
}

void VCLine() {
	int x1;
	int y1;
	int x2;
	int y2;
	int color;

	x1 = ResolveOperand();
	y1 = ResolveOperand();
	x2 = ResolveOperand();
	y2 = ResolveOperand();
	color = ResolveOperand();
	
	Line2d (x1, y1, x2, y2, color);
}

void Line3d (int x1, int y1, int z1, int x2, int y2, int z2, int color) {
	int x1new;
	int y1new;
	int x2new;
	int y2new;

	if (x1 > 2000) {
		x1 = -1*(4000 - x1);
	}
	
	if (y1 > 2000) {
		y1 = -1*(4000 - y1);
	}
	
	if (z1 > 2000) {
		z1 = -1*(4000 - z1);
	}

	if (x2 > 2000) {
		x2 = -1*(4000 - x2);
	}

	if (y2 > 2000) {
		y2 = -1*(4000 - y2);
	}
	
	if (z2 > 2000) {
		z2 = -1*(4000 - z2);
	}
	
	if (z1 == 0) {
		z1++;
	}

	x1new = x1 + 160 + (x1/(z1/256));
	y1new = y1 + 100 + (y1/(z1/256));

	if (z2 == 0) {
		z2++;
	}

	x2new = x2 + 160 + (x2/(z2*256));
	y2new = y2 + 100 + (y2/(z2*256));

	Line2d(x1new, y1new, x2new, y2new, color);
}

void VCLine3d() {
	int x1;
	int y1;
	int z1;
	int x2;
	int y2;
	int z2;
	int color;

	x1 = ResolveOperand();
	y1 = ResolveOperand();
	z1 = ResolveOperand();
	x2 = ResolveOperand();
	y2 = ResolveOperand();
	z2 = ResolveOperand();

	color = ResolveOperand();

	Line3d(x1, y1, z1, x2, y2, z2, color);
}

void execl(char *a, char *b, char *c) {
	// TODO: implement
}

void Exc() {
	char* str1;
	char* str2;

	str1 = (char*)code;
	GrabString(strbuf);
	str2 = (char*)code;
	GrabString(strbuf);

	execl(str1, str1, str2);
}

void GetMagic() {
	short c;
	short d;
	int i;
	int j;
	int alreadyhave = 0;

	c = ResolveOperand() - 1;
	d = ResolveOperand();

	i = 0;

	while (i < 24) {
		if (d == party_stats[c].maginv[i]) {
			alreadyhave = 1;
		}
		i++;
	}

	j = party_stats[c].magcnt;
	if (!alreadyhave) {
		if (j != 24) {
			party_stats[c].maginv[j] = d;
			party_stats[c].magcnt++;
		} else {
			party_stats[c].maginv[j - 1] = d;
		}
	}
}

void VCSpellName() {
	int x1;
	int y1;
	int i;
	int align;

	x1 = ResolveOperand();
	y1 = ResolveOperand();
	i = ResolveOperand();
	align = ResolveOperand();
	
	VCAString(x1, y1, magic[i].name, align);
}

void VCSpellDesc() {
	int x1;
	int y1;
	int i;
	int align;

	x1 = ResolveOperand();
	y1 = ResolveOperand();
	i = ResolveOperand();
	align = ResolveOperand();
	
	VCAString(x1, y1, magic[i].desc, align);
}

void VCSpellImage() {
	int x1;
	int y1;
	int i;
	int gf;
	unsigned char gsimg[512];
	unsigned char* img;

	x1 = ResolveOperand();
	y1 = ResolveOperand();
	i = ResolveOperand();
	gf = ResolveOperand();
	img = magicicons + (magic[i].icon<<8);
	if (gf) { 
		grey(16, 16, img, gsimg);
		img = gsimg;
	}

	VCtcopysprite(x1, y1, 16, 16, img);
}

void MagicShop() {
    int first=1;
    int nv;
    int p;

    playeffect(1);
    an = 1;
    nv = GrabC();
    memset(&storeinv, 0, 12);
    for (p = 0; p < nv; p++) {
        storeinv[p] = ResolveOperand();
    }
    p = 0;
drawloop:
    drawmap();
    PutBuySellBox(p);
    PutGPBox();
    PutCharBox(0, 0, 0, 0, 0, 0);
    
    readcontrols();

    if (first == 2) {
        if (b2 || b3) {
            goto drawloop;
        } else {
            an = 0;
            timer_count = 0;
            return;
        }
    }
    if (first && !b1 && !b3 && !down && !up) {
        first = 0;
    } else if (first) {
        goto drawloop;
    }

    if (down || up) {
        p = p^1;
        playeffect(0);
        first = 1;
    }

    if (b1) {
        if (!p) {
            MBuyMenu();
        }
        if (p) {
            MSellMenu();
        }
    }

    while (!b3 && !b2) {
        goto drawloop;
    }
    while (b3 || b2) {
        first = 2;
        goto drawloop;
    }
}

void PlayVAS() {
    int i;
    int i2;
    int frames;
    int speed;
    int ansave;
    int sizex;
    int sizey;
    int sizeoff;
    int wherex;
    int wherey;
    
    //play_fli(&stringbuffer);
    ansave = an;
    an = 1;

    GrabString(stringbuffer);
    speed = ResolveOperand();
    sizex = ResolveOperand();
    sizey = ResolveOperand();
    wherex = ResolveOperand();
    wherey = ResolveOperand();

    LoadPCXHeaderNP(stringbuffer);

    frames = depth/sizey;
    i = 0;

    sizeoff = sizex*sizey; //calc offset vals

    for (i2 = 0; i2 < depth; i2++) {
        vidoffset = i2*width;
        ReadPCXLine(vcdatabuf);
    }
    fclose(pcxf);

    timer_count = 0;
    while (i < frames) {
        memset(vcscreen, 0, 64000);
        VCtcopysprite(wherex, wherey, sizex, sizey, vcdatabuf + (i*sizeoff));
        drawmap();
        vgadump();

        i2 = 0;
        while (timer_count < (unsigned int)speed) {          
            i2++;
            i2--;
        }
        timer_count = 0;
        i++;
    }
    timer_count = 0;
    an = ansave;
}

void LoadMonster(int mdat, int mnum) {
    // empty
}

void DrawMonster(int monster) {
    // empty
}

void ClearMonster(int monster) {
    int i2;

    strcpy(monsters[monster].name, " ");
    strcpy(monsters[monster].imgfile, " ");
    monsters[monster].curhp=0;
    monsters[monster].curmp=0;
    monsters[monster].maxhp=0;
    monsters[monster].maxmp=0;
    monsters[monster].atk=0;
    monsters[monster].def=0;
    monsters[monster].mag=0;
    monsters[monster].mdef=0;
    monsters[monster].exp=0;
    monsters[monster].gold=0;
    monsters[monster].x=0;
    monsters[monster].y=0;

    i2 = 0;
    while (i2 <= 10) {
        monsters[monster].other[i2] = 0;
        i2++;
    }
}
